ModEditor.GUI.BasicWindow = {}
ModEditor.GUI.BasicWindow.Internal = {}

function ModEditor.GUI.BasicWindow:Draw()
	self.MinimizeButton.X = self.Width-14
	if not self.Visible then
		return
	end
	if self.ShowFullWindow then
		set_color(0,0,0,1)
		draw_quad(self.X,self.Y,self.Width,self.Height)
		set_color(1,1,1,1)
		draw_quad(self.X+1,self.Y+1,self.Width-2,self.Height-2)
	else
		set_color(0,0,0,1)
		draw_quad(self.X,self.Y,self.Width,21)
	end
	if ModEditor.GUI.ActiveWindowId ~= self.Id then
		set_color(0.5,0.5,1,1)
	else
		set_color(0,0,1,1)
	end
	draw_quad(self.X+1,self.Y+1,self.Width-2,20)
	set_color(1,1,1,1)
	draw_text(self.Caption,self.X+2,self.Y+2)
	for i=1,#self.Controls do
		if self.Controls[#self.Controls-i+1].AlwaysVisible or self.ShowFullWindow then
			self.Controls[#self.Controls-i+1]:Draw()
		end
	end
	if self.ShowFullWindow then
		if self.DrawEverything ~= nil then
			self:DrawEverything(self.X+2,self.Y+22,self.Width-4,self.Height-24)
		end
	end
end
function ModEditor.GUI.BasicWindow:Init()
	self.Width = 100
	self.Height = 100
	self.X = 0
	self.Y = 0
	self.Caption = ""
	self.Internal.StartX = 0
	self.Internal.StartY = 0
	self.Internal.Moving = false
	self.ShowFullWindow = false
	self.Id = -1
	self.Visible = false
	self.CurrentControlId = 100500
	self.Controls = {}
	self.MinimizeButton = ModEditor.GUI.Button:New(self)
	self.MinimizeButton.Color = {1,0,0,1}
	self.MinimizeButton.OverColor = {1,0.5,0.5,1}
	self.MinimizeButton.Caption = "X"
	self.MinimizeButton.X = self.Width-14
	self.MinimizeButton.Y = 3
	self.MinimizeButton.Width = 12
	self.MinimizeButton.Height = 16
	self.Child = nil
	self.MinimizeButton.AlwaysVisible = true
	function self.MinimizeButton:OnClick()
		self.Parent.ShowFullWindow = not self.Parent.ShowFullWindow
	end
	self.MinimizeButton.UseBorder = false
	self:AddControl(self.MinimizeButton)
end
function ModEditor.GUI.BasicWindow:MouseDown(x,y,absX,absY)
	if self.Child ~= nil then
		ModEditor.Windows.Activate(self.Child.Id)
		return 1
	end
	for i=1,#self.Controls do
		if self.Controls[i]:InControl(x,y) and (self.Controls[i].AlwaysVisible or self.ShowFullWindow) then
			if (self.Controls[self.CurrentControlId] ~= nil) and (i ~= self.CurrentControlId) then
				self.Controls[self.CurrentControlId]:Deactivate()
			end
			if i ~= self.CurrentControlId then
					self.Controls[i]:Activate()
			end
			self.Controls[i]:MouseDown(x-self.Controls[i].X,y-self.Controls[i].Y)
			self.CurrentControlId = i
			return 1
		end
	end
	if self.Controls[self.CurrentControlId] ~= nil then
		self.Controls[self.CurrentControlId]:Deactivate()
	end
	self.CurrentControlId = -1
	if (y > 0) and (y < 20) then
			self.Internal.StartX = absX
		self.Internal.StartY = absY
		self.Internal.Moving = true
	end
	return 1
end
function ModEditor.GUI.BasicWindow:MouseUp(x,y,absX,absY)
	if self.Internal.Moving then
		self.Internal.Moving = false
		self.Internal.StartX = 0
		self.Internal.StartY = 0
		return 1
	end
	return 0
end
function ModEditor.GUI.BasicWindow:MouseMove(x,y,absX,absY)
	if self.Internal.Moving then
		self.X = self.X+absX-self.Internal.StartX
		self.Y = self.Y+absY-self.Internal.StartY
		self.Internal.StartX = absX
		self.Internal.StartY = absY
		return 1
	end
	return 0
end
function ModEditor.GUI.BasicWindow:MousePositionChange(x,y)
	for i = 1,#self.Controls do
			self.Controls[i]:MousePositionChange(self.Controls[i]:InControl(x-self.X,y-self.Y))
	end
end
function ModEditor.GUI.BasicWindow:InWindow(x,y)
	if self.ShowFullWindow then
		return (x > self.X) and (x < self.X+self.Width) and (y > self.Y) and (y < self.Y+self.Height)
	else
		return (x > self.X)and(x < self.X+self.Width) and (y > self.Y) and (y < self.Y+20)
	end
end
function ModEditor.GUI.BasicWindow:AddControl(control)
	table.insert(self.Controls,1,control)
end
function ModEditor.GUI.BasicWindow:Deactivate()
	if self.Controls[self.CurrentControlId] ~= nil then
		self.Controls[self.CurrentControlId]:Deactivate()
	end
	self.CurrentControlId = -1
end
function ModEditor.GUI.BasicWindow:Show()
	self.Visible = true
	ModEditor.Windows.Activate(self.Id)
end
function ModEditor.GUI.BasicWindow:Hide()
	self.Visible = false
	ModEditor.GUI.ActiveWindowId = 0
end
function ModEditor.GUI.BasicWindow:Apply(parameters)
	for i=1,#parameters do
		self[parameters[i][1]] = parameters[i][2]
	end
end
function ModEditor.GUI.BasicWindow:MouseScrollUp()
	if self.ShowFullWindow then
		if self.Controls[self.CurrentControlId] ~= nil then
			self.Controls[self.CurrentControlId]:MouseScrollUp()
		end
		return 1
	end
	return 0
end
function ModEditor.GUI.BasicWindow:MouseScrollDown()
	if self.ShowFullWindow then
		if self.Controls[self.CurrentControlId] ~= nil then
			self.Controls[self.CurrentControlId]:MouseScrollDown()
		end
		return 1
	end
	return 0
end
function ModEditor.GUI.BasicWindow:Remove()
end
function ModEditor.GUI.BasicWindow:New()
	local obj = {}
	obj.Internal = {}
	obj.Draw = self.Draw
	obj.Init = self.Init
	obj.MouseDown = self.MouseDown
	obj.MouseUp = self.MouseUp
	obj.MouseScrollDown = self.MouseScrollDown
	obj.MouseScrollUp = self.MouseScrollUp
	obj.MouseMove = self.MouseMove
	obj.MousePositionChange = self.MousePositionChange
	obj.InWindow = self.InWindow
	obj.AddControl = self.AddControl
	obj.Deactivate = self.Deactivate
	obj.Apply = self.Apply
	obj.Show = self.Show
	obj.Hide = self.Hide
	obj.Remove = self.Remove
	obj:Init()
	return obj
end
